package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.fc.cp.api.ViewCstoreQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCstoreMapper;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 店仓接口-服务开发
 *
 * @author WSL
 * @create 2017/10/23
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewCstoreQueryCmdImpl extends CommandAdapter implements ViewCstoreQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String cpcDistribId = "CP_C_DISTRIB_ID";
        String storeCate = "STORECATE";
        /*String cpcStoreorgId1 = "CP_C_STOREORG_ID1";
        String cpcStoreorgId2 = "CP_C_STOREORG_ID2";
        String cpcStoreorgId3 = "CP_C_STOREORG_ID3";
        String cpcStoreorgId4 = "CP_C_STOREORG_ID4";
        String cpcStoreorgId5 = "CP_C_STOREORG_ID5";
        String cpcStoreorgId6 = "CP_C_STOREORG_ID6";
        String cpcStoreorgId7 = "CP_C_STOREORG_ID7";
        String cpcStoreorgId8 = "CP_C_STOREORG_ID8";
        String cpcStoreorgId9 = "CP_C_STOREORG_ID9";
        String cpcStoreorgId10 = "CP_C_STOREORG_ID10";*/
        String cpcCityId = "CP_C_CITY_ID";
        String saleCate = "SALECATE";
        String imageLevel = "IMAGELEVEL";
        String modifierId = "MODIFIERID";
        String cpcExpcompanyId = "CP_C_EXPCOMPANY_ID";
        String group = "GROUPID";
        String location = "LOCATION";
        String saleType = "SALETYPE";
        String proCoe = "PROCOE";
        String areaType = "AREATYPE";
        String bagType = "BAGTYPE";
        String producter = "PRODUCTER";
        String bigManager = "BIGMANAGER";
        String prefer = "PREFER";
        String recycleWhId = "RECYCLE_WH_ID";
        String orunify = "ORUNIFY";
        String seaBatch = "SEABATCH";
        String retWhId = "RET_WH_ID";
        String wepayId = "WEPAY_ID";
        String alipayId = "ALIPAY_ID";
        String storeorgId = "CP_C_STOREORG_ID";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCstoreQueryCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0) {
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCstoreMapper viewCstoreMapper = ApplicationContextHandle.getBean(ViewCstoreMapper.class);

        if (!param.containsKey("IDS") || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        // 根据param查询条件 查询【店仓档案】表
        List<HashMap> list = viewCstoreMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap : list) {
            // 判断【配销中心】是否为空 CP_C_DISTRIB_ID
            if (null != hashMap.get(cpcDistribId)) {
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID")));
                hashMap.put("CP_C_DISTRIB_ID", storeorg.getString("ECODE"));
            }
            // 判断【店仓分类】是否为空 STORECATE
            if (null != hashMap.get(storeCate)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("STORECATE")));
                hashMap.put("STORECATE", eCode);
            }
            /*// 判断【店仓组织1】是否为空 CP_C_STOREORG_ID1
            if (null != hashMap.get(cpcStoreorgId1)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID1")));
                hashMap.put("CP_C_STOREORG_ID1",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME1",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织2】是否为空 CP_C_STOREORG_ID2
            if (null != hashMap.get(cpcStoreorgId2)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID2")));
                hashMap.put("CP_C_STOREORG_ID2",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME2",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织3】是否为空 CP_C_STOREORG_ID3
            if (null != hashMap.get(cpcStoreorgId3)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID3")));
                hashMap.put("CP_C_STOREORG_ID3",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME3",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织4】是否为空 CP_C_STOREORG_ID4
            if (null != hashMap.get(cpcStoreorgId4)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID4")));
                hashMap.put("CP_C_STOREORG_ID4",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME4",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织5】是否为空 CP_C_STOREORG_ID5
            if (null != hashMap.get(cpcStoreorgId5)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID5")));
                hashMap.put("CP_C_STOREORG_ID5",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME5",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织6】是否为空 CP_C_STOREORG_ID6
            if (null != hashMap.get(cpcStoreorgId6)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID6")));
                hashMap.put("CP_C_STOREORG_ID6",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME6",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织7】是否为空 CP_C_STOREORG_ID7
            if (null != hashMap.get(cpcStoreorgId7)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID7")));
                hashMap.put("CP_C_STOREORG_ID7",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME7",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织8】是否为空 CP_C_STOREORG_ID8
            if (null != hashMap.get(cpcStoreorgId8)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID8")));
                hashMap.put("CP_C_STOREORG_ID8",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME8",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织9】是否为空 CP_C_STOREORG_ID9
            if (null != hashMap.get(cpcStoreorgId9)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID9")));
                hashMap.put("CP_C_STOREORG_ID9",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME9",storeorg.getString("ENAME"));
            }
            // 判断【店仓组织10】是否为空 CP_C_STOREORG_ID10
            if (null != hashMap.get(cpcStoreorgId10)){
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get("CP_C_STOREORG_ID10")));
                hashMap.put("CP_C_STOREORG_ID10",storeorg.getString("ECODE"));
                hashMap.put("AREA_NAME10",storeorg.getString("ENAME"));
            }*/

            //销售区域组织
            if (null != hashMap.get(storeorgId)) {
                JSONObject storeorg = viewCstoreMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get(storeorgId)));
                //销售区域层级
                Long storeorgLevel = storeorg.getLong("CP_C_STOREORG_LEVEL_ID");
                //上级销售区域
                Long orgUpId = storeorg.getLong("CP_C_ORGUP_ID");
                Integer storeLevel = viewCstoreMapper.selStoreOrgLevel(storeorgLevel);
                storeorg.put("LEVEL", storeLevel);
                ArrayList<JSONObject> list1 = new ArrayList<>();
                list1.add(storeorg);

                list1 = (ArrayList<JSONObject>) storeOrgLevel(viewCstoreMapper, storeorgLevel, orgUpId, list1, storeLevel);

                int listSize = list1.size();
                int maxLevel = 1;
                for (int i = 0; i < listSize; i++) {
                    JSONObject storeOrgJson = list1.get(i);
                    String storeOrgEcode = storeOrgJson.getString("ECODE");
                    String arerEame = storeOrgJson.getString("ENAME");
                    int level = storeOrgJson.getIntValue("LEVEL");

                    if (level > maxLevel) {
                        maxLevel = level;
                    }

                    String ecodeKey = "CP_C_STOREORG_ID" + level;
                    String enameKey = "AREA_NAME" + level;

                    hashMap.put(ecodeKey, storeOrgEcode);
                    hashMap.put(enameKey, arerEame);
                }

                for (int i = maxLevel + 1; i <= 10; i++) {

                    String ecodeKey = "CP_C_STOREORG_ID" + i;
                    String enameKey = "AREA_NAME" + i;

                    hashMap.put(ecodeKey, hashMap.get("CP_C_STOREORG_ID" + maxLevel));
                    hashMap.put(enameKey, hashMap.get("AREA_NAME" + maxLevel));
                }
            }
            // 判断【城市】是否为空 CP_C_CITY_ID
            if (null != hashMap.get(cpcCityId)) {
                String eCode = viewCstoreMapper.selCityEcode(TypeUtils.castToLong(hashMap.get("CP_C_CITY_ID")));
                hashMap.put("CP_C_CITY_ID", eCode);
            }
            // 判断【销售分类】是否为空 SALECATE
            if (null != hashMap.get(saleCate)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("SALECATE")));
                hashMap.put("SALECATE", eCode);
            }
            // 判断【店铺形象级别】是否为空 IMAGELEVEL
            if (null != hashMap.get(imageLevel)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("IMAGELEVEL")));
                hashMap.put("IMAGELEVEL", eCode);
            }
            // 判断【修改人】是否为空 MODIFIERID
            if (null != hashMap.get(modifierId)) {
                String eCode = viewCstoreMapper.selUsersEname(TypeUtils.castToLong(hashMap.get("MODIFIERID")));
                hashMap.put("MODIFIERID", eCode);
            }
            // 判断【快递公司】是否为空 CP_C_EXPCOMPANY_ID
            if (null != hashMap.get(cpcExpcompanyId)) {
                String eCode = viewCstoreMapper.selExpcompanyEcode(TypeUtils.castToLong(hashMap.get("CP_C_EXPCOMPANY_ID")));
                hashMap.put("CP_C_EXPCOMPANY_ID", eCode);
            }
            // 判断【店仓组】是否为空 GROUPID
            if (null != hashMap.get(group)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("GROUPID")));
                hashMap.put("GROUPID", eCode);
            }
            // 判断【店铺属性】是否为空 LOCATION
            if (null != hashMap.get(location)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("LOCATION")));
                hashMap.put("LOCATION", eCode);
            }
            // 判断【销售类型】是否为空 SALETYPE
            if (null != hashMap.get(saleType)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("SALETYPE")));
                hashMap.put("SALETYPE", eCode);
            }
            // 判断【目标款系数】是否为空 PROCOE
            if (null != hashMap.get(proCoe)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("PROCOE")));
                hashMap.put("PROCOE", eCode);
            }
            // 判断【区域属性】是否为空 AREATYPE
            if (null != hashMap.get(areaType)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("AREATYPE")));
                hashMap.put("AREATYPE", eCode);
            }
            // 判断【编织袋类型】是否为空 BAGTYPE
            if (null != hashMap.get(bagType)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("BAGTYPE")));
                hashMap.put("BAGTYPE", eCode);
            }
            // 判断【产品专员】是否为空 PRODUCTER
            if (null != hashMap.get(producter)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("PRODUCTER")));
                hashMap.put("PRODUCTER", eCode);
            }
            // 判断【大区长】是否为空 BIGMANAGER
            if (null != hashMap.get(bigManager)) {
                HashMap ecodeEnameMap = viewCstoreMapper.selEmpEcodeEname(TypeUtils.castToLong(hashMap.get("BIGMANAGER")));

//                String eCode=TypeUtils.castToString(ecodeEnameMap.get("ECODE"));
//                String eName=TypeUtils.castToString(ecodeEnameMap.get("ENAME"));
                if (null != ecodeEnameMap && ecodeEnameMap.size() > 0) {
                    hashMap.put("BIGMANAGER", ecodeEnameMap.get("ECODE"));
                    hashMap.put("CHECK_LEADER", ecodeEnameMap.get("ENAME"));
                } else {
                    hashMap.put("BIGMANAGER", null);
                    hashMap.put("CHECK_LEADER", null);
                }

            }
            // 判断【店铺偏好】是否为空 PREFER
            if (null != hashMap.get(prefer)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("PREFER")));
                hashMap.put("PREFER", eCode);
            }
            // 判断【补货仓】是否为空 RECYCLE_WH_ID
            if (null != hashMap.get(recycleWhId)) {
                String eCode = viewCstoreMapper.selStoreEcode(TypeUtils.castToLong(hashMap.get("RECYCLE_WH_ID")));
                hashMap.put("RECYCLE_WH_ID", eCode);
            }
            // 判断【发退统一】是否为空 ORUNIFY
            if (null != hashMap.get(orunify)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("ORUNIFY")));
                hashMap.put("ORUNIFY", eCode);
            }
            // 判断【转季批次】是否为空 SEABATCH
            if (null != hashMap.get(seaBatch)) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("SEABATCH")));
                hashMap.put("SEABATCH", eCode);
            }
            // 判断【退货仓】是否为空 RET_WH_ID
            if (null != hashMap.get(retWhId)) {
                String eCode = viewCstoreMapper.selStoreEcode(TypeUtils.castToLong(hashMap.get("RET_WH_ID")));
                hashMap.put("RET_WH_ID", eCode);
            }
            // 判断【微信支付账号】是否为空 WEPAY_ID
            if (null != hashMap.get(wepayId)) {
                String eCode = viewCstoreMapper.selMpayaccEcode(TypeUtils.castToLong(hashMap.get("WEPAY_ID")));
                hashMap.put("WEPAY_ID", eCode);
            }
            // 判断【支付宝账号】是否为空 ALIPAY_ID
            if (null != hashMap.get(alipayId)) {
                String eCode = viewCstoreMapper.selMpayaccEcode(TypeUtils.castToLong(hashMap.get("ALIPAY_ID")));
                hashMap.put("ALIPAY_ID", eCode);
            }
            // 【启用】翻译
            if ("N".equals(hashMap.get("ISACTIVE"))) {
                hashMap.put("ISACTIVE", 1);
            } else if ("Y".equals(hashMap.get("ISACTIVE"))) {
                hashMap.put("ISACTIVE", 0);
            }
            // 【店仓类型】翻译
            if ("SW".equals(hashMap.get("STORETYPE"))) {
                hashMap.put("STORETYPE", "sub");
            } else if ("VS".equals(hashMap.get("STORETYPE"))) {
                hashMap.put("STORETYPE", "virtual");
            }
            // 【经营状态】翻译
            if ("NOR".equals(hashMap.get("BUSSTATE"))) {
                hashMap.put("BUSSTATE", "01");
            } else if ("DEC".equals(hashMap.get("BUSSTATE"))) {
                hashMap.put("BUSSTATE", "02");
            } else if ("CLOSE".equals(hashMap.get("BUSSTATE"))) {
                hashMap.put("BUSSTATE", "03");
            }
            // 【城市级别】翻译 TXTDIM3
            if ("1".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "一线城市");
            } else if ("2".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "二线城市");
            } else if ("3".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "三线城市");
            } else if ("4".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "四线城市");
            } else if ("5".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "五线城市");
            } else if ("6".equals(hashMap.get("TXTDIM3"))) {
                hashMap.put("TXTDIM3", "六线城市");
            }
            // 【商圈类型】翻译 NUMDIM2
            if (null != hashMap.get("NUMDIM2")) {
                String eName = viewCstoreMapper.selStoredimItemEname(TypeUtils.castToLong(hashMap.get("NUMDIM2")));
                hashMap.put("NUMDIM2", eName);
            }

            // 【店铺类别】翻译 NUMDIM1
            if (null != hashMap.get("NUMDIM1")) {
                String eCode = viewCstoreMapper.selStoredimItemEcode(TypeUtils.castToLong(hashMap.get("NUMDIM1")));
                hashMap.put("NUMDIM1", eCode);
            }

            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }
        vh.put("data", resultJson);
        return vh;
    }

    /**
     * @param viewCstoreMapper MAPPER
     * @param storeorgLevelID  层级数
     * @param orgUpId          上级销售区域ID
     */
    private List<JSONObject> storeOrgLevel(ViewCstoreMapper viewCstoreMapper, Long storeorgLevelID,
                                           Long orgUpId, ArrayList list, Integer storeLevel) {

        if (null != storeLevel && storeLevel > 1) {
            JSONObject storeOrgJson = viewCstoreMapper.selStoreorgEcode(orgUpId);
            storeorgLevelID = storeOrgJson.getLong("CP_C_STOREORG_LEVEL_ID");
            orgUpId = storeOrgJson.getLong("CP_C_ORGUP_ID");
            storeLevel = viewCstoreMapper.selStoreOrgLevel(storeorgLevelID);
            storeOrgJson.put("LEVEL", storeLevel);
            list.add(storeOrgJson);
            return storeOrgLevel(viewCstoreMapper, storeorgLevelID, orgUpId, list, storeLevel);
        } else {
            return list;
        }
    }


}
